using System;
using System.Collections.Generic;
using NHibernate;

namespace FwkLight.Domain.Query
{
  public abstract class NamedQuery<T> : NamedQuery<T, T>
  {
  }

  public abstract class NamedQuery<TIN, TOUT> : IDomainQuery<TIN, TOUT>
  {
    private IQuery _query;
    public IDomainQuery<TIN, TOUT> Initialise ( ISession session )
    {
      _query = session.GetNamedQuery(QueryName);
      SetParameters(_query);
      return this;
    }

    public IDomainQuery<TIN, TOUT> Initialise(ISession session, bool includingInactive)
    {
      throw new NotImplementedException("You can't set active / inactive from here. Use AddDependency of that task, then SetParameters(IQuery query).");
    }

    protected abstract void SetParameters(IQuery query);

    protected abstract string QueryName { get;}

    public virtual TOUT GetResult()
    {
    	throw new NotImplementedException();
    }

  	public TIN UniqueResult()
    {
      return _query.UniqueResult<TIN>();
    }

		public TIN UniqueResult(ISession session)
		{
			Initialise(session);
			return _query.UniqueResult<TIN>();
		}

    protected IList<TIN> LoadList()
    {
      return _query.List<TIN>();
    }

		protected IList<TIN> LoadList(ISession session)
		{
			Initialise(session);
			return LoadList();
		}
  }
}